package com.example.invoker.schedule.govee;

import com.biapp.appboard.mapper.installation.CircadianRhythmMapper;
import com.biapp.appboard.mapper.installation.OtherTimesMapper;
import com.biapp.appboard.pojo.model.installation.CircadianRhythmTimes;
import com.biapp.appboard.pojo.model.installation.OtherTimes;
import com.biapp.common.util.ZonedDateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @Author huaiyu yin
 * @Date 2021/7/14 3:01 下午
 * @Version 1.0
 */
@Service
public class CircadianRhythmSyncService extends BaseSyncService{
    /**
     * 1.获取db昼夜节律数据
     * 2.验证数据
     * 3.组装完整的othertimes数据
     * 4.数据处理
     */

    @Autowired
    private CircadianRhythmMapper circadianRhythmMapper;
    @Autowired
    private OtherTimesMapper otherTimesMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void process(String startDate, String endDate) {
        this.validParam(startDate,endDate);
        save(assemblyData(""));
    }

    public Collection assemblyData(String str) {
        Integer startTime = ZonedDateUtil.convertStrgDateToDateInt(startDate);
        Integer endTime = ZonedDateUtil.convertStrgDateToDateInt(endDate);
        //获取统计的昼夜节律数量
        List<CircadianRhythmTimes> listUseNum = circadianRhythmMapper.getListUseNum(startTime, endTime);
        List<OtherTimes>  reslut = new ArrayList<>();
        if(listUseNum.isEmpty()){
            return reslut;
        }
        listUseNum.forEach(item->{
            OtherTimes vo = new OtherTimes();
            vo.setDailyDate(item.getDailyDate());
            String daily = ZonedDateUtil.convertDateIntToStrDate(item.getDailyDate());
            vo.setDailyYear(item.getDailyDate()/10000);
            vo.setDailyMonth(ZonedDateUtil.getIntYearMonth(daily));
            vo.setDailyWeek(ZonedDateUtil.getIntYearWeek(daily));
            vo.setTypes(4);
            vo.setUseNum(item.getUseNum());
            reslut.add(vo);
        });
        return reslut;
    }


    @Override
    public void save(Collection entityList) {
        if(entityList.isEmpty()){
            return ;
        }
        otherTimesMapper.batchInsert((List<OtherTimes>) entityList);

    }
}
